package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.Setmeal;
import com.itheima.service.RoleService;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @Autowired
    private JedisPool jedisPool;


    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
        } catch (Exception e) {
           // e.printStackTrace();
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        }
      return new Result(false,MessageConstant.ADD_ROLE_FAIL);
    }

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());

        return pageResult;
    }


    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        if(role != null){
            Result result = new Result(true, MessageConstant.QUERY_ROLE_SUCCESS);
            result.setData(role);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
    }

    //根据检查组id查询检查项id
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> roleIds = roleService.findMenuByRoleId(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //根据检查组id查询检查项id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> roleIds = roleService.findPermissonByRoleId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,roleIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds , Integer[] menuIds){

        try {
            roleService.edit(role,permissionIds,menuIds);
        } catch (Exception e) {
            //e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }


    @RequestMapping("/findAll")
    public Result findAll(){

        List<Role> roleList =  roleService.findAll();
        if(roleList != null && roleList.size()>0){
            Result result = new Result(true,MessageConstant.QUERY_ROLE_SUCCESS);
            result.setData(roleList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_ROLE_FAIL);

    }

}
